package com.yixinsoft.basic.web.controller.manage.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yixinsoft.basic.model.manage.system.Organization;
import com.yixinsoft.basic.model.manage.system.Resource;
import com.yixinsoft.basic.service.manage.system.OrganizationService;

/**
 * Created by hqy on 2016/06/05
 */
@Controller
@RequestMapping("/manage/system/organization")
public class OrganizationController {

	@Autowired
	private OrganizationService organizationService;
	
	@RequiresPermissions("organization:view")
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) {
		return "manage/system/organization/index";
	}
	
	@RequiresPermissions("organization:view")
	@RequestMapping(value = "/tree", method = RequestMethod.GET)
	public String tree(Model model) {
		model.addAttribute("organizationList", organizationService.queryList(null));
		return "manage/system/organization/tree";
	}
	
	@RequiresPermissions("organization:create")
	@RequestMapping(value = "/appendChild/{parentId}", method = RequestMethod.GET)
	public String appendChild(Model model, @PathVariable Long parentId) {
		Organization parent = organizationService.queryById(parentId);
		Resource child = new Resource();
		child.setParentId(parentId);
		child.setParentIds(parent.makeSelfAsParentIds());

		model.addAttribute("parent", parent);
		model.addAttribute("child", child);
		return "manage/system/organization/appendChild";
	}
	
	@RequiresPermissions("organization:create")
	@RequestMapping(value = "/appendChild/{parentId}", method = RequestMethod.POST)
	public String appendChild(Organization organization) {
		organizationService.save(organization);
		return "redirect:/manage/system/organization/success";
	}
	
	@RequiresPermissions("organization:update")
	@RequestMapping(value = "/main/{id}", method = RequestMethod.GET)
	public String update(Model model, @PathVariable Long id) {
		model.addAttribute("organization", organizationService.queryById(id));
		return "manage/system/organization/main";
	}
	
	@RequiresPermissions("organization:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	public String update(Organization organization) {
		organizationService.update(organization);
		return "redirect:/manage/system/organization/success";
	}
	
	@RequiresPermissions("organization:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public String delete(@PathVariable Long id) {
		organizationService.deleteById(id);
		return "redirect:/manage/system/organization/success";
	}
	
	@RequiresPermissions("organization:update")
	@RequestMapping(value = "/move/{sourceId}", method = RequestMethod.GET)
	public String move(Model model, @PathVariable Long sourceId) {
		Organization source = organizationService.queryById(sourceId);
		model.addAttribute("source", source);
		model.addAttribute("targetList", organizationService.findAllWithExclude(source));
		return "manage/system/organization/move";
	}
	
	@RequiresPermissions("organization:update")
	@RequestMapping(value = "/move/{sourceId}", method = RequestMethod.POST)
	public String move(@PathVariable Long sourceId, @RequestParam Long targetId) {
		Organization source = organizationService.queryById(sourceId);
		Organization target = organizationService.queryById(targetId);
		organizationService.move(source, target);
		return "redirect:/manage/system/organization/success";
	}
	
	@RequiresPermissions("organization:view")
	@RequestMapping(value = "/success", method = RequestMethod.GET)
	public String success() {
		return "manage/system/organization/success";
	}
}
